﻿using System.Diagnostics;
using WPFCommon.Interface;
using WPFCommon.ViewModels.Base;

namespace WPFCommon.ViewModels.Misc
{
    public class ProfilerViewModel : ViewModelBase
    {
        #region private fields

        private readonly Stopwatch _stopwatch = new Stopwatch();
        private long _elapsedMilliseconds;
        private string _name;

        public ProfilerViewModel(string name)
        {
            Name = name;
        }

        #endregion

        #region prop

        public string Name
        {
            get => _name;
            set
            {
                _name = value;
                OnPropertyChanged();
            }
        }

        public long ElapsedMilliseconds
        {
            get => _elapsedMilliseconds;
          private  set
            {
                _elapsedMilliseconds = value;
                OnPropertyChanged();
            }
        }

        public IDataLogger DataLogger { get; set; }

        #endregion

        #region api

        public void Restart()
        {
            _stopwatch.Restart();
        }
        
        public void Stop()
        {
            _stopwatch.Stop();
            ElapsedMilliseconds = _stopwatch.ElapsedMilliseconds;
            DataLogger?.Log(ElapsedMilliseconds);
        }

        #endregion
    }
}